package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.exception.R;
import com.hqyj.pojo.UserInfo;
import com.hqyj.dao.UserInfoMapper;
import com.hqyj.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.util.MdFive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-12-07 05:14:05
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    UserInfoMapper userInfoMapper;

    @Autowired
    MdFive mdFive;
    @Override
    public R regist(UserInfo u) {

        /*---验证用户名不能重名---*/
        QueryWrapper<UserInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_name",u.getUserName());
        UserInfo u1 = userInfoMapper.selectOne(queryWrapper1);
        if (u1!=null){
            log.info("用户名重名");
            return R.error("用户名重名");
        }
        /*---验证手机号是否已注册过---*/
        QueryWrapper<UserInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_phone",u.getUserPhone());
        UserInfo u2 = userInfoMapper.selectOne(queryWrapper2);
        if (u2!=null){
            log.info("此手机号已经被注册");
            return R.error("此手机号已经被注册");
        }
        /*---验证邮箱是否已注册过---*/
        QueryWrapper<UserInfo> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("user_email",u.getUserEmail());
        UserInfo u3 = userInfoMapper.selectOne(queryWrapper3);
        if (u3!=null){
            log.info("此邮箱已经被注册");
            return R.error("此邮箱已经被注册");

        }
       /*-----密码加密处理-----*/
        //利用UUID生产盐值
        String salt = UUID.randomUUID()+"";
        //存入盐值
        u.setUserSalt(salt);
        //Md5加密密码
        String pwd = mdFive.getPassword(u.getUserPwd(),salt);
        //存入加密后的密码
        u.setUserPwd(pwd);
        int num =userInfoMapper.insert(u);
        if (num==0){
            log.info("注册失败");
            return R.error("注册失败");

        }
        log.info("注册成功");
        return R.ok("注册成功",u);
    }
}
